/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TextInput,
  Dimensions,
  TouchableOpacity,
  AsyncStorage
} from 'react-native';

import styles1 from './styles/Pager.style';
import { colors } from './styles/Pager.style';

import DropdownAlert from 'react-native-dropdownalert'

var Header = require("./Component/Header");
var net = require("./net/NetUitl");
var _navigator;

var user_id;

class FeedBack extends Component {
	constructor(props) {  
		super(props);
		_navigator=this.props.navigator;
		
		this._sumbit = this._sumbit.bind(this);
		this._netCallBack = this._netCallBack.bind(this);
		this._errorCallBack = this._errorCallBack.bind(this);
		
		this.state ={
			msg:"",
			contactor:"",
		}
  	}
	
	componentDidMount() {
		_this = this;
		
		AsyncStorage.getItem("user_id",function(errors,result){
			if(result != null){
				user_id = result;
			}
		});
	}
	
	async _netCallBack(data){
		if(data.code === "1"){
			this.dropdown.alertWithType('info', '提示', '感谢您宝贵的意见，我们会及时处理');
		}
	}
	
	_errorCallBack(){

	}
		
	_sumbit(){
		if(this.state.msg === ""){
			this.dropdown.alertWithType('warn', '提示', '请填写您的意见');
			return;
		}else if(this.state.contactor === ""){
			this.dropdown.alertWithType('warn', '提示', '请填写您的联系方式');
			return;
		}
		
		let formData = new FormData();  
		formData.append("user_id",user_id);  
		formData.append("msg",this.state.msg);  
		formData.append("linker",this.state.contactor);
		var fetchOptions = {
          	method: 'POST',
          	headers: {     
          		'Accept': 'application/json',
          	},
          	body: formData
       	}; 
       	
		net.postFrom("http://www.idolly.com/help/index/index"
				,fetchOptions
				,this._netCallBack 
				,this._errorCallBack);
	}
	
	onClose(data) {
	// data = {type, title, message}
	}
	
	render() {
	    return (
	      	<View style={styles1.container}>
	      		<Header name="意见反馈" IconLeft="chevron-left" onPressLeft={()=> _navigator.pop()}/>
		      	<View>
			      	<TextInput
					    multiline={true}
					    blurOnSubmit={true}
					    returnKeyType ='done'
					    placeholder="请输入您宝贵的意见"
					    style={styles.texts}
					    underlineColorAndroid="transparent"
					    maxLength={200}
					    numberOfLines={4}
					    editable = {true}
					    value={this.state.msg}
					    onChangeText={(text) => this.setState({msg:text})}
					/>
			      	<TextInput 
			      		style={styles.textinput}
			      		value={this.state.contactor}
			      		placeholder="请输入手机号/QQ/邮箱"
			      		underlineColorAndroid={"#6c1ddc"}
					    onChangeText={(text) => this.setState({contactor:text})}
			      	/>
			      	<TouchableOpacity onPress={()=>this._sumbit()}>
			        	<View style={styles1.button}>
			        		<Text style={styles1.findText}>提交</Text>
			        	</View>
			        </TouchableOpacity>
		      	</View>
		      	<DropdownAlert ref={(ref) => this.dropdown = ref} onClose={(data) => this.onClose(data)} />
	      	</View>
	    );
	}
}

const styles = StyleSheet.create({
	container: {
	    flex: 1,
	    justifyContent: 'center',
	    alignItems: 'center',
	    backgroundColor: '#F5FCFF',
	},
	texts:{
		width:Dimensions.get('window').width-20,
		height:140,
		borderWidth:1,
		borderColor:"#6c1ddc",
		backgroundColor:"white",
		margin:10,
		fontSize:16
	},
	textinput:{
		width:Dimensions.get('window').width-20,
		margin:10,
		fontSize:16
	}
});

module.exports = FeedBack;
